package com.wt.controller;

import com.wt.service.OrderService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;

@WebServlet("/DelOrderServlet")
public class DelOrderServlet extends HttpServlet {
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //设置编码
        response.setContentType("application/json;charset=utf-8");
        response.setCharacterEncoding("utf-8");
        request.setCharacterEncoding("utf-8");
        PrintWriter out = response.getWriter();

        //参数获取和验证
        String parameter = request.getParameter("orderId");
        if (parameter == null || "".equals(parameter)){
            String data = "{\"data\":\"nullParameter\"}";
            out.write(data);
            out.flush();
            return;
        }
        try {
            Integer orderId = Integer.valueOf(parameter);
            //参数验证通过，执行业务
            OrderService orderService = new OrderService();
            String data;
            Boolean res = orderService.delOrder(orderId);
            if (res){
                data = "{\"data\":true}";
            }else {
                data = "{\"data\":false}";
            }
            out.write(data);
            out.flush();
        } catch (SQLException | NumberFormatException e) {
//            e.printStackTrace();
            String data = "\"data\":\"errorParameter\"";
            out.write(data);
            out.flush();
        }
    }
}
